﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.Collections;
using System.ComponentModel;

namespace QuanLyRapChieuPhim.DAO
{
    class TinhTrangPhongChieuDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<TinhTrangPhongChieuDTO> GetList()
        {

            DataTable dt = new DataTable();
            BindingList<TinhTrangPhongChieuDTO> ds = new BindingList<TinhTrangPhongChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_TinhTrangPhongChieu", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    TinhTrangPhongChieuDTO TinhTrangPhongChieu = new TinhTrangPhongChieuDTO();
                    TinhTrangPhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrangPhongChieu"].ToString());
                    TinhTrangPhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    TinhTrangPhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(TinhTrangPhongChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<TinhTrangPhongChieuDTO> GetList_QL()
        {

            DataTable dt = new DataTable();
            BindingList<TinhTrangPhongChieuDTO> ds = new BindingList<TinhTrangPhongChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_TinhTrangPhongChieu_QL", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    TinhTrangPhongChieuDTO TinhTrangPhongChieu = new TinhTrangPhongChieuDTO();
                    TinhTrangPhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrangPhongChieu"].ToString());
                    TinhTrangPhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    TinhTrangPhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(TinhTrangPhongChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<TinhTrangPhongChieuDTO> GetList_QL_DaXoa()
        {

            DataTable dt = new DataTable();
            BindingList<TinhTrangPhongChieuDTO> ds = new BindingList<TinhTrangPhongChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_TinhTrangPhongChieu_QL_DaXoa", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    TinhTrangPhongChieuDTO TinhTrangPhongChieu = new TinhTrangPhongChieuDTO();
                    TinhTrangPhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrangPhongChieu"].ToString());
                    TinhTrangPhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    TinhTrangPhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(TinhTrangPhongChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<TinhTrangPhongChieuDTO> GetListLikeName_QL(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<TinhTrangPhongChieuDTO> ds = new BindingList<TinhTrangPhongChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_TinhTrangPhongChieu_QL", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    TinhTrangPhongChieuDTO TinhTrangPhongChieu = new TinhTrangPhongChieuDTO();
                    TinhTrangPhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrangPhongChieu"].ToString());
                    TinhTrangPhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    TinhTrangPhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(TinhTrangPhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<TinhTrangPhongChieuDTO> GetListLikeName_QL_DaXoa(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<TinhTrangPhongChieuDTO> ds = new BindingList<TinhTrangPhongChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_TinhTrangPhongChieu_QL_DaXoa", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    TinhTrangPhongChieuDTO TinhTrangPhongChieu = new TinhTrangPhongChieuDTO();
                    TinhTrangPhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrangPhongChieu"].ToString());
                    TinhTrangPhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    TinhTrangPhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(TinhTrangPhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public int GetIDFromName(string _Ten)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ten", _Ten) };
                dt = dp.SqlExcuteQuery("uproc_GetIDFromName_TinhTrangPhongChieu", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count > 0)
                    return int.Parse(dt.Rows[0][0].ToString());
                return 0;
            }
            return -1;
        }

        public TinhTrangPhongChieuDTO GetFromId(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetFromID_TinhTrangPhongChieu", sqlParameter);
                dp.CloseConnect();
                TinhTrangPhongChieuDTO TinhTrangPhongChieu = new TinhTrangPhongChieuDTO();
                foreach (DataRow row in dt.Rows)
                {
                    TinhTrangPhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrangPhongChieu"].ToString());
                    TinhTrangPhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                }
                return TinhTrangPhongChieu;
            }
            return null;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_TinhTrangPhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_TinhTrangPhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(TinhTrangPhongChieuDTO _TinhTrangPhongChieu)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ten", _TinhTrangPhongChieu.TenTinhTrangPhongChieu) };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_TinhTrangPhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Update(TinhTrangPhongChieuDTO _TinhTrangPhongChieu)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _TinhTrangPhongChieu.MaTinhTrangPhongChieu), new SqlParameter("@Ten", _TinhTrangPhongChieu.TenTinhTrangPhongChieu) };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_TinhTrangPhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

    }
}
